//
//  SpotInfoWeightView.swift
//  swiftProject
//
//  Created by ZhangSai on 2021/7/6.
//

import UIKit

class SpotInfoWeightView: UIView, UITextFieldDelegate{

    @IBOutlet weak var valuelab: UILabel!
    @IBOutlet weak var weightTF: UITextField!
    @IBOutlet weak var wideTF: UITextField!
    @IBOutlet weak var heightTF: UITextField!
    @IBOutlet weak var lenghtTF: UITextField!
    
    @IBOutlet weak var weightLab: UILabel!
    @IBOutlet weak var volumeLab: UILabel!
    @IBOutlet weak var hintLab: UILabel!
    @IBOutlet weak var weightView: UIView!

    var orderInfoModel:OrderListModel!
    
    
    typealias SpotInfoWeightBlick = () -> Void
    var spotInfoWeightBlick : SpotInfoWeightBlick!

    override func awakeFromNib() {
        super.awakeFromNib()
        self.weightView.layer.borderWidth = 0.5
        self.layer.borderColor = UIColor.init(hexString: "#7E7E7E").cgColor
        hintLab.text = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_actual_weight)
        self.setValue()
    }
    func setValue() {
        weightLab.text = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_estimateWeight)
        volumeLab.text = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_estimateVolume)
        lenghtTF.placeholder = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_length)
        heightTF.placeholder = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_height)
        wideTF.placeholder = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_width)
        weightTF.text = "1"
        
        lenghtTF.delegate = self
        wideTF.delegate = self
        lenghtTF.delegate = self
        weightTF.delegate = self
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        self.endEditing(true)
    }
    
    func setOrderInfoModel(orderInfoModel:OrderListModel) {
        self.orderInfoModel = orderInfoModel
        lenghtTF.text = orderInfoModel.goodsLength > 0 ? String(orderInfoModel.goodsLength) : ""
        wideTF.text = orderInfoModel.goodsWidth > 0 ? String(orderInfoModel.goodsWidth) : ""
        heightTF.text = orderInfoModel.goodsHeight > 0 ? String(orderInfoModel.goodsHeight) : ""
        weightTF.text = orderInfoModel.goodsWeight == 0 ? String(orderInfoModel.goodsWidth) : ""
        if orderInfoModel.goodsSize.count > 0 {
            valuelab.text = "=" + orderInfoModel.goodsSize
        } else {
            let doubleValue:Double = orderInfoModel.goodsLength * orderInfoModel.goodsWidth * orderInfoModel.goodsHeight / 1728
            valuelab.text = "=" + String.init(format: "%.2f", doubleValue)
        }
    }
    
    @IBAction func clickWeightBtn(_ sender: UIButton) {
        self.endEditing(true)
        let index:Int
        if sender.tag == 100 {
            
            index = Int(weightTF.text ?? "") ?? 0 - 1
            if index > 0 {
                weightTF.text = String.init(format: "%ld", index)
            } else {
                
            }
        } else if sender.tag == 101 {
            index = Int(weightTF.text ?? "") ?? 0 + 1
            if index < Constants.Spot_weigt_value {
                weightTF.text = String.init(format: "%ld", index)
            } else {
                Constants.window?.makeToast(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_hint_weight_exceed))
            }
        }
        self.weightTFChange()
    }
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        let cs:CharacterSet = CharacterSet.init(charactersIn: "0123456789.").inverted
        let filtered = string.components(separatedBy: cs).joined(separator: "")
        if string != filtered {
            return false
        }
        let totalStr:String = textField.text ?? "" + string
        if totalStr == "0" {
            return false
        }
        if weightTF == textField {
            self.perform(#selector(weightTFChange), with: self, afterDelay: 0.5)
            if Int(totalStr) ?? 0 > Constants.Spot_weigt_value {
                Constants.window?.makeToast(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_hint_weight_exceed))
                return false
            }
        } else {
            if Int(totalStr) ?? 0  > 27648 {
                Constants.window?.makeToast(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_hint_length_exceed))
            }
        }
        return true
    }
    
    @objc func weightTFChange() {
        if (spotInfoWeightBlick != nil) {
            spotInfoWeightBlick()
        }
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        if lenghtTF == textField || wideTF == textField || heightTF == textField {
            if  lenghtTF.text?.count ?? 0 > 0 && wideTF.text?.count ?? 0 > 0 && heightTF.text?.count ?? 0 > 0 {
                let lenghtValue = Double(lenghtTF.text ?? "") ?? 0
                let wideValue = Double(wideTF.text ?? "") ?? 0
                let heightValue = Double(heightTF.text ?? "") ?? 0

                let value:Double = lenghtValue * wideValue * heightValue / 1728
                if value > 16 {
                    valuelab.text = "16.00"
                    Constants.window?.makeToast(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_hint_volume_exceed))
                } else {
                    valuelab.text = String(format: "%.2f",value)
                }
            }
        }
    }
}
